package com.antdesignbackend.backend.configure.dict.controller;

import java.io.Serializable;
import java.util.Date;

import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.configure.dict.entity.Dict;
import com.antdesignbackend.backend.configure.dict.entity.entityVo.DictVo;
import com.antdesignbackend.backend.configure.dict.service.DictService;

/**
 * 字典管理
 *
 * @author 不二
 * @email jack.com
 * @date 2020-07-22 15:52:05
 */

@Controller
@RequestMapping("/dict")
@Slf4j
public class DictController extends BaseController{

	private static final String FUN_NAME = "字典管理";//功能名称

	@Autowired
	private DictService dictService;

	@GetMapping("/index")
	@AuthToken
	public String index() {
		return "configure/dict/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody DictVo dictVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		QueryWrapper<Dict> wrapper = new QueryWrapper<>();
		Page<Dict> page = new Page<>(dictVo.getPageIndex(), dictVo.getPageSize());
		IPage<Dict> pageResult = dictService.page(page, wrapper);
		result.setTotal((int)pageResult.getTotal());
		result.setData(pageResult.getRecords());
		return result;

	}

	@GetMapping("/getById/{id}")
	@ResponseBody
	public	Dict getById(@PathVariable(name = "id") Long id) {
		Dict obj = dictService.getById(id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody Dict dict){
		ReturnVO result = new ReturnVO();
		//设置创建者和创建时间
		dict.setCreate_by(getLoginUserId());
		dict.setCreate_date(new Date());
		try {
			if(dictService.save(dict)){
				result.setMessage("保存成功！");
				result.setCode("200");
			}else{
				result.setMessage("保存失败！");
				result.setCode("500");
			}
		}catch (Exception e){
			log.error(e.getMessage());
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody Dict dict){
		ReturnVO result = new ReturnVO();
		//设置修改人和修改时间
		dict.setUpdate_by(getLoginUserId());
		dict.setUpdate_date(new Date());
		try {
			if(dictService.updateById(dict)){
				result.setMessage("保存成功！");
				result.setCode("200");
			}else{
				result.setMessage("保存失败！");
				result.setCode("500");
			}
		}catch (Exception e){
			log.error(e.getMessage());
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (dictService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

}
